<template>
  <el-dialog :title="!dataForm.id ? '新增测评指标' : '修改测评指标'" :close-on-click-modal="false" :visible.sync="visible" width="800px" top="8vh">
    <el-container>
      <el-main style="max-height:70vh">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm"  label-width="140px">
          <el-form-item label="测评项统称" prop="xmMc">
            <span>{{dataForm.xmMc}}</span>
          </el-form-item>
          <el-form-item label="上一级指标" prop="pMc">
            <span>{{dataForm.pMc}}</span>
          </el-form-item>
          <el-form-item label="指标项名称" prop="zbMc">
            <el-input v-model="dataForm.zbMc" placeholder="指标项名称" style="width:90%;"></el-input>
          </el-form-item>
          <el-form-item label="提示内容" prop="tsNr">
            <el-input type="textarea" :rows="5" v-model="dataForm.tsNr" placeholder="提示内容" style="width:90%;"></el-input>
          </el-form-item>
          <el-form-item label="指标排序" prop="zbPx">
            <!-- <el-input v-model="dataForm.zbPx" placeholder="指标排序" type="number" oninput="value=value.replace(/[^\d]/g,'')"></el-input> -->
            <!-- <el-slider v-model="dataForm.zbPx" :step="1" :max="30" show-stops></el-slider> -->
            <el-input-number  v-model="dataForm.zbPx"   style="width:200px" :min="0"  :max="10000"></el-input-number>
          </el-form-item>
          <el-form-item label="指标权重" prop="zbQz">
            <el-input-number v-model="dataForm.zbQz" :precision="2" :step="1" :max="100" :min="0" style="width:200px" :disabled="weightMark"></el-input-number>%
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
    <span slot="footer" class="dialog-footer">
      
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      <el-button type="primary" plain @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getObj, addObj, putObj, selzjList } from '@/api/ceping/cepingxmmxb'

export default {
  data() {
    return {
      weightMark: false,
      visible: false,
      canSubmit: false,
      dataForm: {},
      dataRule: {
        zbMc: [
          { required: true, message: '指标名称不能为空', trigger: 'blur' },
          { min:1, max:200, message: '长度在 1 到 200 个字符', trigger: 'blur' }
        ],
        tsNr: [
          { required: true, message: '提示内容不能为空', trigger: 'blur' },
          { min:1, max:4000, message: '长度在 1 到 4000 个字符', trigger: 'blur' }
        ],
        zbQz: [
          { required: true, message: '指标权重不能为空', trigger: 'blur' }
        ],
        zbPx: [
          { required: true, message: '指标排序不能为空', trigger: 'blur' },
          //  { min:1, max:11, message: '长度在 1 到 11 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id, pId, name) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.canSubmit = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          getObj(this.dataForm.id).then(response => {
            response.data.data.tsNr = response.data.data.tsNr && decodeURIComponent(decodeURIComponent( response.data.data.tsNr));
            this.dataForm = response.data.data
            this.$set(this.dataForm, 'xmMc', name);
            if (pId == 0) {
              this.$set(this.dataForm, 'pMc', '无');
            } else {
              getObj(pId).then(response => {
                this.$set(this.dataForm, 'pMc', response.data.data.zbMc);
              })
            }
          });
          selzjList(id).then(response => {
            if (response.data.data.length > 0) {
              this.weightMark = true;
            }else{
              this.weightMark = false;
            }
          });
        }
      })
    },
    //父级触发增加
    initFj(data) {
      this.weightMark = false;
      this.visible = true;
      this.canSubmit = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields();
        this.dataForm = JSON.parse(JSON.stringify(data));
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          if (this.dataForm.id) {//&& this.dataForm.tsNr.replaceAll('\n','<br>')&& this.dataForm.tsNr.replaceAll('\r\n','\\r\\n')
            // this.dataForm.tsNr = this.dataForm.tsNr 
            this.dataForm.tsNr = encodeURIComponent(encodeURIComponent(this.dataForm.tsNr));
            putObj(this.dataForm).then(data => {
              this.$notify.closeAll();
              this.$notify.success('修改成功');
              this.visible = false;
              this.$emit('refreshDataList');
            }).catch(() => {
              this.canSubmit = true;
            });
          } else {
            addObj(this.dataForm).then(data => {
              this.$notify.closeAll();
              this.$notify.success('添加成功');
              this.visible = false;
              this.$emit('refreshDataList');
            }).catch(() => {
              this.canSubmit = true;
            });
          }
        }
      })
    }
  }
}
</script>
